//
//  DrawCubeDemoVC.m
//  FilamentDemo
//
//  Created by 8000 on 2020/5/1.
//  Copyright © 2020 tencent. All rights reserved.
//

#import "DrawCubeDemoVC.h"
#import "FilamentView.h"
#import "FilamentRender.h"
#import "FilamentConvenientHeader.h"
#import "Entities.h"
#import "FPSCameraController.h"
@interface DrawCubeDemoVC ()

@end

@implementation DrawCubeDemoVC
{
    float3 *_vertexs;
    uint16_t *_indices;
    Entity _cubeEntity;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    int vertexCount = 8;
    int indicesCount = 36;
    _vertexs = new float3[vertexCount]{
        float3{-0.1,  0.1, 0.1},
        float3{ 0.1,  0.1, 0.1},
        float3{ 0.1, -0.1, 0.1},
        float3{-0.1, -0.1, 0.1},

        float3{-0.1, 0.1, -0.1},
        float3{ 0.1, 0.1, -0.1},
        float3{ 0.1,-0.1, -0.1},
        float3{-0.1,-0.1, -0.1},
    };
    _indices = new uint16_t[indicesCount]{
        0,3,2, //前
        0,2,1,

        5,6,7, //后
        5,7,4,

        0,4,7, //左
        0,7,3,

        1,2,6, //右
        1,6,5,

        4,0,1, //顶
        4,1,5,

        6,2,3, //底
        6,3,7
    };

	VertexBuffer *vb = VertexBuffer::Builder()
              .vertexCount(vertexCount)
              .bufferCount(1)
              //描述buffer内存布局,第二个参数是 buffer 索引
              .attribute(VertexAttribute::POSITION,
                         0,
                         VertexBuffer::AttributeType::FLOAT3,
                         0,//从缓冲区哪里开始 bufferOffset
                         sizeof(float3) //Stride 该属性数据段的间隔
                         )
//              .attribute(VertexAttribute::COLOR, 1, VertexBuffer::AttributeType::FLOAT3, 0, sizeof(float3))
              .build(*self.render.engine);

    //给buffer设置数据
    vb->setBufferAt(*self.render.engine,
                    //VertexBuffer::Builder() attribute 函数第二个参数设置的索引，表示给顶点的哪个属性设置数据
                    0,
                    VertexBuffer::BufferDescriptor(_vertexs,
                                                   vertexCount * sizeof(float3), nullptr));
    
//    vb->setBufferAt(*self.render.engine,
//                       1,
//                       VertexBuffer::BufferDescriptor(_colors,
//                                                      vertexCount * sizeof(float3), nullptr));

    IndexBuffer *ib = IndexBuffer::Builder().indexCount(indicesCount)
											.bufferType(IndexBuffer::IndexType::USHORT)
											.build(*self.render.engine);

    ib->setBuffer(*self.render.engine, IndexBuffer::BufferDescriptor(_indices,
                                                                     indicesCount * sizeof(uint16_t), nullptr));
    _cubeEntity = EntityManager::get().create();
    
    Box aabb = RenderableManager::computeAABB(_vertexs, _indices, vertexCount);
    
    filament::RenderableManager::Builder(1)
                        .geometry(0, filament::RenderableManager::PrimitiveType::TRIANGLES, vb, ib,
                                  0,//buffer start reading offset
                                  indicesCount)
                        //包围盒设置，用于视锥剔除计算，若不设置就要关闭剔除开关。
                        .boundingBox(aabb)
                        .build(*self.render.engine, _cubeEntity);
    

    self.render.scene->addEntity(_cubeEntity);
}

- (void)dealloc{
    [self.render removeEntity:_cubeEntity];
    if (_vertexs != nullptr) {
        delete _vertexs;
        _vertexs = nullptr;
    }
    if (_indices != nullptr) {
        delete _indices;
        _indices = nullptr;
    }
}
@end
